package com.nameless.service;

import com.nameless.entity.Client;
import com.nameless.entity.MemberLevel;
import com.nameless.entity.Menu;
import com.nameless.entity.Users;
import com.nameless.feign.ClientFeign;
import com.nameless.feign.MemberLevelFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author WHZ
 * @date 2023年03月02日 10:23
 */
@Service
public class UserService implements UserDetailsService {
    @Autowired
    private ClientFeign clientFeign;
    @Autowired
    private MemberLevelFeign memberLevelFeign;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        Client admin = clientFeign.getUsername(username);
        if (admin.getStatus().equals(0)) {
            if (admin != null) {
                MemberLevel permissions = memberLevelFeign.selectMemberlevel(admin.getMemberLevelId());
                List<GrantedAuthority> collect = new ArrayList<>();
                collect.add(new SimpleGrantedAuthority(permissions.getName()));
                User client = new User(admin.getUsername(), admin.getPassword(), collect);
                return client;
            }
        }else {
            return null;
        }

        return null;
    }

}
